﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.ArrivalCheck;
using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.CheckCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.CheckCommandHandler
{
    public class AddCheckRecordCommandHandler:IRequestHandler<AddCheckRecordCommand,APIResult<object>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<ArrivalCheck> arrivalCheckBiz;
        private readonly IBaseRepository<CheckRecord> checkRecordBiz;
        private readonly IBaseRepository<OrderMaterials> orderMaterialsBiz;

        public AddCheckRecordCommandHandler(IMapper mapper, IBaseRepository<ArrivalCheck> arrivalCheckBiz, IBaseRepository<CheckRecord> checkRecordBiz, IBaseRepository<OrderMaterials> orderMaterialsBiz)
        {
            this.mapper = mapper;
            this.arrivalCheckBiz = arrivalCheckBiz;
            this.checkRecordBiz = checkRecordBiz;
            this.orderMaterialsBiz = orderMaterialsBiz;
        }

        public Task<APIResult<object>> Handle(AddCheckRecordCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            //添加  添加成功分析   修改
            var arrivalCheckModel = mapper.Map<CheckRecord>(request);
            if(arrivalCheckModel.NotQualifiedNum + arrivalCheckModel.QualifiedNum == arrivalCheckModel.CalulateNumber)
              {
                  arrivalCheckModel.CheckStatus = 1;
                  int res = checkRecordBiz.Add(arrivalCheckModel).Result;
                  if (res > 0)
                  {
                      result.data = arrivalCheckModel;
                      result.Code = APIEnum.添加成功;
                      result.Msg = APIEnum.添加成功.ToString();
                    //添加成功后开始汇总到货检验单据的状态
                    //获取所有该到货检验单据的所有物料集合
                    var materialList = orderMaterialsBiz.Quary(x => x.SaleCode == request.PurchaseInRepository_ID && x.MaterialsClass == 2).Result;
                    //获取所有该到货检验单据的所有物料集合
                    var checkRecordList = checkRecordBiz.Quary(x => x.PurchaseInRepository_ID == request.PurchaseInRepository_ID).Result;
                    int total = (int)materialList.Sum(x => x.CalulateNumber);
                    int hege = checkRecordList.Sum(x => x.QualifiedNum);
                    int nohege = checkRecordList.Sum(x => x.NotQualifiedNum);
                    if (hege + nohege == total) 
                    {
                        //获取来源是采购入库的到货检验单信息
                        var arrivalCheck = arrivalCheckBiz.Quary(x => x.SourceId == request.PurchaseInRepository_ID).Result.FirstOrDefault();
                        arrivalCheck.CheckStatus = 1;
                        arrivalCheckBiz.Update(arrivalCheck);
                    }
                 
                  }
                  else
                  {
                      result.Code = APIEnum.添加失败;
                      result.Msg = APIEnum.添加失败.ToString();
                  }
            }
            else
            {
                result.Code = APIEnum.合格数量加上不合格数量不等于物料总数;
                result.Msg = APIEnum.合格数量加上不合格数量不等于物料总数.ToString();
            }
            return Task.FromResult(result); 
        }
    }
}
